//Milvetti
//@version=5
indicator(title='Alex Project', shorttitle='Alex Project', max_bars_back=500, max_labels_count=500, max_lines_count=500)

// Existing inputs
n1 = input(10, 'Channel Length')
n2 = input(21, 'Average Length')

ap = hlc3
esa = ta.ema(ap, n1)
d = ta.ema(math.abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ta.ema(ci, n2)

wt1 = tci
wt2 = ta.sma(wt1, 4)

plot(0, color=color.new(color.gray, 0))
plot(wt1, color=color.new(color.green, 0))
plot(wt2, color=color.new(color.red, 0))
plot(wt1 - wt2, color=color.new(color.blue, 50), style=plot.style_columns)

crossup = wt1 > wt2 and (wt1 < 0 or wt2 < 0)
crossdown = wt1 < wt2 and (wt1 > 0 or wt2 > 0)

plots = "=== Plots ==="
bullColor = input.color(color.green, title="Bull Color", group=plots)
bearColor = input.color(color.red, title="Bear Color", group=plots)
showZones = input(true, "Show Zones", tooltip="Show Zones When All Candles Green Or Red", group=plots)

timeFrames = "=== Timeframes ==="
onTf1 = input(true, " ", inline="1", group=timeFrames)
timeframe1 = input("2", 'Timeframe 1', inline="1", group=timeFrames)
onTf2 = input(true, " ", inline="2", group=timeFrames)
timeframe2 = input("3", 'Timeframe 2', inline="2", group=timeFrames)
onTf3 = input(true, " ", inline="3", group=timeFrames)
timeframe3 = input("5", 'Timeframe 3', inline="3", group=timeFrames)
onTf4 = input(true, " ", inline="4", group=timeFrames)
timeframe4 = input("6", 'Timeframe 4', inline="4", group=timeFrames)
onTf5 = input(true, " ", inline="5", group=timeFrames)
timeframe5 = input("7", 'Timeframe 5', inline="5", group=timeFrames)
onTf6 = input(true, " ", inline="6", group=timeFrames)
timeframe6 = input("8", 'Timeframe 6', inline="6", group=timeFrames)
onTf7 = input(true, " ", inline="7", group=timeFrames)
timeframe7 = input("9", 'Timeframe 7', inline="7", group=timeFrames)
onTf8 = input(true, " ", inline="8", group=timeFrames)
timeframe8 = input("10", 'Timeframe 8', inline="8", group=timeFrames)
onTf9 = input(true, " ", inline="9", group=timeFrames)
timeframe9 = input("15", 'Timeframe 9', inline="9", group=timeFrames)
onTf10 = input(true, " ", inline="10", group=timeFrames)
timeframe10 = input("18", 'Timeframe 10', inline="10", group=timeFrames)

// Momentum Length Input
momentumLength = input.int(10, minval=1, title="Momentum Length")

// Function to calculate Heikin-Ashi color (Unused in this script)
calculateHeikinColor(timeframe) =>
    ha_t = ticker.heikinashi(syminfo.tickerid)
    ha_open = request.security(ha_t, str.tostring(timeframe), open, lookahead=barmerge.lookahead_on)
    ha_close = request.security(ha_t, str.tostring(timeframe), close, lookahead=barmerge.lookahead_on)
    ha_close > ha_open ? bullColor : bearColor

// Heikin-Ashi Open and Close for each timeframe
// BotTF1
ha_open0 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe1, open, lookahead=barmerge.lookahead_on)
ha_close0 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe1, close, lookahead=barmerge.lookahead_on)

// BotTF2
ha_open1 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe2, open, lookahead=barmerge.lookahead_on)
ha_close1 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe2, close, lookahead=barmerge.lookahead_on)

// BotTF3
ha_open2 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe3, open, lookahead=barmerge.lookahead_on)
ha_close2 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe3, close, lookahead=barmerge.lookahead_on)

// BotTF4
ha_open3 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe4, open, lookahead=barmerge.lookahead_on)
ha_close3 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe4, close, lookahead=barmerge.lookahead_on)

// BotTF5
ha_open4 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe5, open, lookahead=barmerge.lookahead_on)
ha_close4 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe5, close, lookahead=barmerge.lookahead_on)

// BotTF6
ha_open5 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe6, open, lookahead=barmerge.lookahead_on)
ha_close5 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe6, close, lookahead=barmerge.lookahead_on)

// BotTF7
ha_open6 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe7, open, lookahead=barmerge.lookahead_on)
ha_close6 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe7, close, lookahead=barmerge.lookahead_on)

// BotTF8
ha_open7 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe8, open, lookahead=barmerge.lookahead_on)
ha_close7 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe8, close, lookahead=barmerge.lookahead_on)

// BotTF9
ha_open8 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe9, open, lookahead=barmerge.lookahead_on)
ha_close8 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe9, close, lookahead=barmerge.lookahead_on)

// BotTF10
ha_open9 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe10, open, lookahead=barmerge.lookahead_on)
ha_close9 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe10, close, lookahead=barmerge.lookahead_on)

// Buy conditions
buy1 = onTf1 ? ha_open0 <= ha_close0 : true
buy2 = onTf2 ? ha_open1 <= ha_close1 : true
buy3 = onTf3 ? ha_open2 <= ha_close2 : true
buy4 = onTf4 ? ha_open3 <= ha_close3 : true
buy5 = onTf5 ? ha_open4 <= ha_close4 : true
buy6 = onTf6 ? ha_open5 <= ha_close5 : true
buy7 = onTf7 ? ha_open6 <= ha_close6 : true
buy8 = onTf8 ? ha_open7 <= ha_close7 : true
buy9 = onTf9 ? ha_open8 <= ha_close8 : true
buy10 = onTf10 ? ha_open9 <= ha_close9 : true

// Sell conditions
sell1 = onTf1 ? ha_open0 >= ha_close0 : true
sell2 = onTf2 ? ha_open1 >= ha_close1 : true
sell3 = onTf3 ? ha_open2 >= ha_close2 : true
sell4 = onTf4 ? ha_open3 >= ha_close3 : true
sell5 = onTf5 ? ha_open4 >= ha_close4 : true
sell6 = onTf6 ? ha_open5 >= ha_close5 : true
sell7 = onTf7 ? ha_open6 >= ha_close6 : true
sell8 = onTf8 ? ha_open7 >= ha_close7 : true
sell9 = onTf9 ? ha_open8 >= ha_close8 : true
sell10 = onTf10 ? ha_open9 >= ha_close9 : true

// Combined signals
greenSig = buy1 and buy2 and buy3 and buy4 and buy5 and buy6 and buy7 and buy8 and buy9 and buy10
redSig = sell1 and sell2 and sell3 and sell4 and sell5 and sell6 and sell7 and sell8 and sell9 and sell10

// Background colors for zones
bgcolor(greenSig and showZones ? color.new(color.lime, 70) : na)
bgcolor(redSig and showZones ? color.new(color.red, 70) : na)

// Buy and Sell triggers
buyTrigger = crossup and greenSig
sellTrigger = crossdown and redSig

// Momentum calculation for 15-minute timeframe
momentum_current = request.security(syminfo.tickerid, '15', ta.mom(close, momentumLength), lookahead=barmerge.lookahead_on)
momentum_prev = request.security(syminfo.tickerid, '15', ta.mom(close, momentumLength)[1], lookahead=barmerge.lookahead_on)

// Buy and Sell signals with Momentum condition
buySignal = buyTrigger and not buyTrigger[1] and (momentum_current > momentum_prev)
sellSignal = sellTrigger and not sellTrigger[1] and (momentum_current < momentum_prev)

// Plot Buy and Sell signals
plotshape(buySignal, "Buy Signal", shape.labelup, location.belowbar, color.green, text="Buy", textcolor=color.white, size=size.auto, force_overlay=true)
plotshape(sellSignal, "Sell Signal", shape.labeldown, location.abovebar, color.red, text="Sell", textcolor=color.white, size=size.auto, force_overlay=true)

// Alert conditions
alertcondition(buySignal, "Buy Signal", "New Buy Signal")
alertcondition(sellSignal, "Sell Signal", "New Sell Signal")